package com.nj.interfaces.actions.uploadpicture.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.nj.framework.util.SpringMVCResult;
import com.nj.interfaces.actions.uploadpicture.service.UserPictureService;
import com.nj.interfaces.actions.uploadpicture.vo.ReturnUploadHeadPictureVO;
import com.nj.interfaces.actions.uploadpicture.vo.UploadHeadPictureVO;

/**
 * 项目名称： LawSchool 类名称： UploadPictureAction 描述：
 * 
 * @author 吴志虎 创建时间： 2016年1月14日 下午6:51:00
 * 
 */
public class UploadPictureAction {

	private static final Log logger = LogFactory.getLog(UploadPictureAction.class);
	@Autowired
	private UserPictureService pictureService;

	/**
	 * 
	 * uploadMobileUserHeaderPicture 移动端上传用户头像图片
	 * 
	 * @param
	 * @return
	 * @throws
	 * @since CodingExample　Ver(编码范例查看) 1.1
	 *//*
	@SuppressWarnings("finally")
	@RequestMapping("/uploadMobileUserHeaderPicture.action")
	public ResponseEntity<String> uploadMobileUserHeaderPicture(HttpServletRequest request, HttpServletResponse response) {
		String userCode = request.getParameter("userCode");// 上传用户
		String imageName = request.getParameter("imageName");// 图片名称，例如moon.jpg

		UploadHeadPictureVO vo = new UploadHeadPictureVO();
		ReturnUploadHeadPictureVO result = new ReturnUploadHeadPictureVO();
		result.setFlag("0");
		Throwable ept = null;
		String operStatus = "失败";// 接口是否调用成功
		String errorMessage = null;// 存放可能的异常信息
		String originImgUrl = null;// 存放原始图片的相对路径
		String cutImgUrl = null;// 存放裁剪后的图片的相对路径
		String imageId = null;// 生成好的图片ID
		String picUrl = null;// 图片服务器上的图片相对路径

		try {
			if (StringUtils.isBlank(userCode)) {
				result.setFailReason("000066");// 用户ID不能为空
				return null;
			}

			// 上传原始图片至服务器上，供系统进行裁剪
			Map<String, Object> uploadOriginalResult = pictureService.uploadMobileOriginalPicture(request);
			if ("0".equals(MapUtils.getString(uploadOriginalResult, "operFlag"))) {
				result.setFailReason(MapUtils.getString(uploadOriginalResult, "operCode"));
				errorMessage = MapUtils.getString(uploadOriginalResult, "errorMessage");
				return null;
			}
			originImgUrl = MapUtils.getString(uploadOriginalResult, "imgUrl");
			logger.info("原始用户头像图片url：" + originImgUrl);

			// 保存文件至图片服务器
			Map<String, Object> upload2ServerResult = null;
			upload2ServerResult = pictureService.upload2ImageServer(request, originImgUrl, false);
			if ("0".equals(MapUtils.getString(upload2ServerResult, "operFlag"))) {
				result.setFailReason(MapUtils.getString(upload2ServerResult, "operCode"));
				errorMessage = MapUtils.getString(upload2ServerResult, "errorMessage");
				return null;
			}
			imageId = MapUtils.getString(upload2ServerResult, "imgId");
			picUrl = MapUtils.getString(upload2ServerResult, "picUrl");
			logger.info("上传至图片服务器上，ID：" + imageId + "  路径：" + picUrl);

			pictureService.removeTempFile(request, originImgUrl, cutImgUrl);// 删除此前生成的临时图片
			pictureService.modifyUserHeaderId(userCode, imageId);// 修改数据库中用户的头像图片id
			result.setFlag("1");
		} catch (Exception e) {
			result.setFailReason("000039");
			logger.error("上传用户头像接口异常，参数：" + imageName);
			logger.error("上传用户头像接口异常，信息：", e);
			ept = e;
		} finally {
			if ("1".equals(result.getFlag())) {
				operStatus = "成功";
				vo.setImageId(imageId);
				vo.setPicUrl(picUrl);
				result.setData(vo);
				result.setFailReason("");
			}
			ResponseEntity<String> entity = SpringMVCResult.returnResponseEntity(JSON.toJSONString(result));
			return entity;
		}
	}*/

	/*
	 * public String LoadImageToServer(String filePath,String serverPath) throws Exception {
	 * 
	 * String resultPath = ""; //上传后图片所在的路径 FileOutputStream out = null; //文件输出流 try { //验证图片上传的格式是否正确 File f = new
	 * File(filePath); if (!f.isFile()) { throw new Exception(f " 不是图片文件!"); } if (f != null && f.exists()) {
	 * //这里的ImageIO属于java工厂类，在工厂类class里面，调用的System.gc()，频繁调用会造成dump，需要考虑优化 BufferedImage image = ImageIO.read(f); //
	 * 读入文件 if (image != null) { BufferedImage tag = new BufferedImage(image.getWidth(), image.getHeight(),
	 * BufferedImage.TYPE_INT_RGB); //构造一个类型为预定义图像类型之一的 BufferedImage tag.getGraphics().drawImage(image, 0, 0,
	 * image.getWidth(), image.getHeight(), null); //绘制所需要尺寸大小的图片
	 * 
	 * 以下生成图片上传后在服务器上的新路径
	 * 
	 * int lastLength = filePath.lastIndexOf("."); Date date = new Date(System.currentTimeMillis()); String strDate =
	 * new SimpleDateFormat("yyyyMMddhhmmss").format(date); int random = (int)(Math.random()*99); String imageName =
	 * strDate random; //以系统时间来随机的创建图片文件名 String fileType = filePath.substring(lastLength); //获取上传图片的类型 resultPath =
	 * serverPath "site" imageName fileType;
	 * 
	 * 进行图片的绘制
	 * 
	 * out = new FileOutputStream(resultPath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
	 * JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag); param.setQuality(0.95f, true); //95%图像
	 * param.setDensityUnit(1); //像素尺寸单位.像素/英寸 param.setXDensity(300); //水平分辨率 param.setYDensity(300); //垂直分辨率
	 * encoder.setJPEGEncodeParam(param); encoder.encode(tag); tag = null; } }
	 * 
	 * f = null;
	 * 
	 * } catch (Exception ex) { ex.printStackTrace(); } finally { out.close(); out = null; } return resultPath; }
	 */
}